﻿/// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

$(document).ready(function ()
{
    // check the need for async list view
    var asyncListViewButtons = $("BUTTON[name=asyncComboView], BUTTON[name=addNewItems]");
    if (asyncListViewButtons == null || asyncListViewButtons.length == 0)
    {
        return;
    }

    // append dialog div into body
    var dialog = $("<DIV>").addClass("asyncListView").addClass("hidden");
    $("BODY").append(dialog);

    // define dialog
    $(".asyncListView").dialog({
        autoOpen: false,
        height: 500,
        width: 800,
        modal: true,
        buttons: {
            "Select": function (e)
            {
                dialog.data("select")(e, $(this));
            },
            "Filter": function (e)
            {
                FilterListView(e, $(this));
            },
            Cancel: function ()
            {
                $(this).dialog("close");
            }
        },
        open: function (e)
        {
            LoadListView(e, $(this));
        },
        close: function (e)
        {
            e.preventDefault();
            $(this).html("");
        }
    });

    $("BUTTON[name=asyncComboView]").live('click', function (e)
    {
        e.preventDefault();
        var dialogWindow = $(".asyncListView");
        // when selected radio is confirmed
        dialogWindow.data("select", SelectOneItem);


        dialogWindow.data("viewSettings", "MP=cnt&RowRenderMode=Radio");
        dialogWindow.data("url", $(this).data("asyncurl"));

        dialogWindow.data("targetInput", $(this).prev());

        dialogWindow.dialog("open", e);
    });
    $("BUTTON[name=addNewItems]").live('click', function (e)
    {
        e.preventDefault();

        var dialogWindow = $(".asyncListView");
        // when selected radio is confirmed
        dialogWindow.data("select", SelectMoreItems);

        dialogWindow.data("viewSettings", "MP=cnt&RowRenderMode=Checkbox");
        dialogWindow.data("url", $(this).data("asyncurl"));

        dialogWindow.data("target", $(this).parents("FIELDSET"));
        dialogWindow.data("nameforaddeditem", $(this).data("nameforaddeditem"));

        dialogWindow.dialog("open", e);
    });
});

function SelectMoreItems(e, dialog)
{
    e.preventDefault();
    var table = $("#asyncTable", dialog);
    if (table == null || table.length != 1)
    {
        return false;
    }
    var checkboxes = $(":checked[name=checkbox]", table);

    var target = dialog.data("target");
    var newItemsTable = $("TABLE.newMembers", target);
    //var newItemsTable = $(target).nextAll("TABLE.newMembers");
    for (var i = 0; i < checkboxes.length; i++)
    {
        var inp = checkboxes[i];

        var input = $("<INPUT>");
        input.attr("type", "hidden");
        input.attr("name", dialog.data("nameforaddeditem"));
        input.attr("value", inp.value);

        target.append(input);

        var row = $("<TR>");
        var idCell = $("<TD>");
        var descriptionCell = $("<TD>");

        idCell.html(inp.value);
        descriptionCell.html($(inp).data("displayname"));

        row.append(idCell);
        row.append(descriptionCell);
        newItemsTable.append(row);
    }

    dialog.dialog("close", e);
    return false;
}
function SelectOneItem(e, dialog)
{
    // try to find the table with Radios
    e.preventDefault();
    var table = $("#asyncTable", dialog);
    if (table == null || table.length != 1)
    {
        return false;
    }
    // find checked radio
    var radio = $(":checked[name=radio]", table).first();
    if (radio == undefined || radio.length == 0)
    {
        return false;
    }

    // set hidden field value to Radio.Value
    var description = $(radio).data("displayname");
    var targetInput = dialog.data("targetInput");
    targetInput.val(radio.val());

    // adjust the text of the Anchor
    var anchorDescription = targetInput.prevUntil("A");
    if (anchorDescription != null && anchorDescription.length > 0)
    {
        anchorDescription.html(description);
    }
    // adjust the value of the AutoComplete input
    var inputAutoComplete = targetInput.prevAll("INPUT.autoComplete");
    if (inputAutoComplete != null && inputAutoComplete.length > 0)
    {
        inputAutoComplete.val(description);
    }

    dialog.dialog("close", e);
    return false;
}

function LoadListView(e, dialog)
{
    e.preventDefault();
    var url = $(dialog).data("url");
    $("A", dialog).live('click', function (evnt)
    {
        evnt.preventDefault();
        dialog.load($(this).attr("href"), dialog.data("viewSettings"));
    });
    dialog.load(url, dialog.data("viewSettings"));
}

function FilterListView(e, dialog)
{
    e.preventDefault();
    var form = $("FORM", dialog);

    var url = form.attr('action');
    url += url.indexOf("?") >= 0 ? "&" : "?";
    url += dialog.data("viewSettings");

    if (form != null && form.length == 1)
    {
        $.ajax({
            type: form.attr('method'),
            url: url,
            data: form.serialize(),
            success: function (data)
            {
                dialog.html(data);
            }
        });
    }
}